﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Infrastructure
{
    /// <summary>
    /// 公共数据转换帮助类。Add by scb ,2020 8 3
    /// </summary>
    public static partial class ObjectExtensions
    {
        public static bool IsEmpty(this object[] me)
        {
            if (me == null) return true;

            if (me.Length == 0) return true;

            return false;
        }

        public static bool IsNullOrDBNull(this object me)
        {
            return !(me != null && me != DBNull.Value);
        }

        public static T As<T>(this object me)
        {
            return As<T>(me, default(T));
        }

        public static T As<T>(this object me, T defaultValue)
        {
            T v;
            try
            {
                v = (T)Convert.ChangeType(me, typeof(T));
            }
            catch
            {
                v = defaultValue;
            }
            return v;
        }

        public static DateTime? TryParseToDateTime(this object me)
        {
            if (me.IsNullOrDBNull())
                return null;
            else if (me is DateTime)
                return (DateTime)me;
            else
            {
                DateTime v;
                if (DateTime.TryParse(me.ToString(), out v))
                    return v;
                else
                    return null;
            }
        }

        public static string TryParseToString(this object me)
        {
            return me == null ? string.Empty : Convert.ToString(me);
        }

        public static bool TryParseToBool(this object me, bool defaultValue = false)
        {
            if (me.IsNullOrDBNull())
                return defaultValue;
            if (me is int)
            {
                return (int)me > 0 ? true : false;
            }
            else
            {
                bool v;
                bool.TryParse(me.ToString(), out v);
                return v;
            }
        }

        public static int TryParseToInt(this object me, int defaultValue = 0)
        {
            int v = defaultValue;
            decimal dv = defaultValue;
            if (!(me.IsNullOrDBNull()))
            {
                if (decimal.TryParse(me.ToString(), out dv))
                {
                    if (dv >= int.MinValue && dv <= int.MaxValue)
                        v = decimal.ToInt32(dv);
                    else
                        v = defaultValue;
                }
                else
                {
                    v = defaultValue;
                }
            }

            return v;
        }

        public static long TryParseToLong(this object me, long defaultValue = 0)
        {
            long v = defaultValue;
            decimal dv = defaultValue;
            if (!me.IsNullOrDBNull())
            {
                if (decimal.TryParse(me.ToString(), out dv))
                {
                    if (dv >= long.MinValue && dv <= long.MaxValue)
                        v = decimal.ToInt64(dv);
                    else
                        v = defaultValue;
                }
                else
                {
                    v = defaultValue;
                }
            }
            return v;
        }

        public static decimal TryParseToDecimal(this object me, decimal defaultValue = 0)
        {
            decimal v = defaultValue;
            if (!me.IsNullOrDBNull())
            {
                if (!decimal.TryParse(me.ToString(), out v))
                {
                    v = defaultValue;
                }
            }
            return v;
        }

        public static double TryParseToDouble(this object me, double defaultValue = 0)
        {
            double v = defaultValue;
            if (!me.IsNullOrDBNull())
            {
                if (!double.TryParse(me.ToString(), out v))
                {
                    v = defaultValue;
                }
            }
            return v;
        }
    }
}
